package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.dao.service.BaseAppDAO;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.dao.SysRolePostDAO;
import com.ruoyi.system.domain.SysRolePost;
import com.ruoyi.system.service.ISysRolePostService;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;

/**
 * 角色和职责关联信息Service业务层处理
 *
 * @author youde
 * @date 2022-11-16
 */
@Service
public class SysRolePostServiceImpl implements ISysRolePostService {
    @Autowired
    private SysRolePostDAO sysRolePostDAO;

    @Autowired
    private BaseAppDAO baseDAO;


    /**
     * 查询角色和职责关联信息
     *
     * @param rolePostId 角色和职责关联信息主键
     * @return 角色和职责关联信息
     */
    @Override
    public SysRolePost selectSysRolePostByRolePostId(String rolePostId) {
        return sysRolePostDAO.findById(rolePostId).orElse(null);
    }

    /**
     * 查询角色和职责关联信息列表
     *
     * @param sysRolePost 角色和职责关联信息
     * @return 角色和职责关联信息
     */
    @Override
    public Page<SysRolePost> selectSysRolePostList(SysRolePost sysRolePost, Pageable pageinfo) {
        return sysRolePostDAO.findAll(getSpecification(sysRolePost), pageinfo);
    }

    /**
     * 查询角色和职责关联信息列表
     *
     * @param sysRolePost 角色和职责关联信息
     * @return 角色和职责关联信息
     */
    @Override
    public List<SysRolePost> selectSysRolePostList(SysRolePost sysRolePost) {
        return sysRolePostDAO.findAll(getSpecification(sysRolePost));
    }


    private Specification<SysRolePost> getSpecification(SysRolePost sysRolePost) {
        return (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();

            if (!StringUtils.isNull(sysRolePost.getRoleId())) {
                predicateList.add(criteriaBuilder.equal(root.get("roleId"), sysRolePost.getRoleId()));
            }
            if (!StringUtils.isNull(sysRolePost.getPostId())) {
                predicateList.add(criteriaBuilder.equal(root.get("postId"), sysRolePost.getPostId()));
            }
            if (!StringUtils.isEmpty(sysRolePost.getPostname())) {
                predicateList.add(criteriaBuilder.like(criteriaBuilder.upper(root.get("postname")), "%" + StringUtils.upperCase(sysRolePost.getPostname()) + "%"));
            }
            if (!StringUtils.isEmpty(sysRolePost.getPostcode())) {
                predicateList.add(criteriaBuilder.equal(root.get("postcode"), sysRolePost.getPostcode()));
            }
            Predicate[] p = new Predicate[predicateList.size()];
            return criteriaBuilder.and(predicateList.toArray(p));
        };

    }

    /**
     * 新增角色和职责关联信息
     *
     * @param sysRolePost 角色和职责关联信息
     * @return 结果
     */
    @Transactional
    @Override
    public SysRolePost[] insertSysRolePost(SysRolePost[] sysRolePost) {
        if (StringUtils.isNotEmpty(sysRolePost)) {
            sysRolePostDAO.deleteByRoleId(sysRolePost[0].getRoleId());
            return sysRolePostDAO.saveAll(Arrays.asList(sysRolePost)).toArray(new SysRolePost[0]);
        }
        return null;

    }

    /**
     * 修改角色和职责关联信息
     *
     * @param sysRolePost 角色和职责关联信息
     * @return 结果
     */
    @Override
    public SysRolePost updateSysRolePost(SysRolePost sysRolePost) {
        return sysRolePostDAO.save(sysRolePost);
    }

    /**
     * 批量删除角色和职责关联信息
     *
     * @param rolePostIds 角色和职责关联信息id
     * @return 结果
     */
    @Transactional
    @Override
    public void deleteSysRolePostByIds(String[] rolePostIds) {
        sysRolePostDAO.deleteAllByIdInBatch(Arrays.asList(rolePostIds));
    }

    /**
     * 删除角色和职责关联信息信息
     *
     * @param roleId 角色和职责关联信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public void deleteSysRolePostByRoleId(String roleId) {
        sysRolePostDAO.deleteByRoleId(roleId);
    }

    /**
     * 根据角色id查询关联的岗位信息
     *
     * @param roleId
     * @return
     */
    @Override
    public List<SysRolePost> findByRoleId(String roleId) {
        return sysRolePostDAO.findByRoleId(roleId);
    }

    /**
     * 更加岗位id查询关联的角色信息
     *
     * @param postId
     * @return
     */
    @Override
    public List<SysRolePost> findByPostId(String postId) {
        return sysRolePostDAO.findByPostId(postId);
    }

    /**
     * 根据岗位id查询关联的角色编码、角色名称、所属公司等信息
     *
     * @param postId
     * @return
     */
    @Override
    public List<SysRolePost> findRoleInfoByPostId(String postId) {
        String querysql = "select " +
                " sys_role_post.role_post_id as rolePostId," +
                " sr.role_id as roleId," +
                " sr.role_key as roleKey," +
                " sr.role_name as roleName," +
                " sc.corp_name as corpName," +
                " sys_role_post.create_by as createBy," +
                " sys_role_post.create_time as createTime," +
                " sys_role_post.post_id as postId " +
                " from sys_role_post " +
                " left join sys_role sr on sys_role_post.role_id = sr.role_id " +
                " left join sys_corp sc on sr.pk_corp = sc.pk_corp " +
                " where sys_role_post.post_id= '" + postId + "'";
        return baseDAO.createNativeQuery(querysql);

    }
}